<?php

class ItemController extends Controller
{
	//搜索和分类的列表
	public function actionList()
	{
            $cate = null;
           if(!isset($_GET['cid']) || $_GET['cid'] == 50015824){
					if($_GET['cid'] == 50015824){
						$_GET['keyword']='青少年家具';
					}
                    if(!isset($_GET['keyword'])){
                            $this->render('noinput');
                            Yii::app()->end();
                    }else{
                            $keyword = CHtml::encode($_GET['keyword']);
							$py = new Pinyin();
							$filename = $py->str2py($keyword);
							if(!$filename){
								$filename = date('Ymd',time());
							}
							
							$totalItems = 390;//10页
							$pager = new CPagination($totalItems);
							$pager->pageSize = Yii::app()->params['tbapicfg']['page_size'];
							$pagenumber = $pager->getCurrentPage()+1;
							$js_cache_file = $filename.'_search_'.$pagenumber.'.js';
                            $this->render('search',array('kw'=>$keyword,'pager'=>$pager,'pagenumber'=>$pagenumber,'jsfile'=>$js_cache_file));
                            Yii::app()->end();
                    }
            }

            $cid = (int)$_GET['cid'];
            $cate = Cates::model()->find('cid=:cid',array(':cid'=>$cid));

            if($cate->parent_cid==0){
                    $parent = $cate;
            }else{
                    $parent = Cates::model()->find('cid=:cid',array(':cid'=>$cate->parent_cid));
            }

            $totalItems = min(3900,$cate->count);
            $pager = new CPagination($totalItems);
            $pager->pageSize = Yii::app()->params['tbapicfg']['page_size'];

            $sort = 'credit_desc';
            if(isset($_GET['sort']) && in_array($_GET['sort'],array('credit_desc','commissionNum_desc'))){
                    $sort = $_GET['sort'];
            }

            $newcfg = array(
                    'method' => 'taobao.taobaoke.items.get',
                    'fields' => 'num_iid,title,pic_url,volume,price,shop_click_url,seller_credit_score',
                    'cid' => $cate->cid,
                    'sort' => $sort,
                    'page_no' => $pager->getCurrentPage()+1,
                    'outer_code'=>'miyifun',
            );
            $s = new TaobaoApi($newcfg);
            $arr = $s->getData();

            $cate->click = ++$cate->click;
            $cate->count = min(3900,$arr['taobaoke_items_get_response']['total_results']);
            $cate->save();

            $this->render('list',array('data'=>$arr,'pager'=>$pager,'title'=>$cate->name,'parent'=>$parent));
	}
	//查看单个商品
	public function actionDetail(){
		$this->layout = '//layouts/bigright';
		
		$iid = trim($_GET['iid']);
		//item detail
		$newcfg = array(
			'method' => 'taobao.taobaoke.items.detail.get',
			'fields' => 'cid,click_url,shop_click_url,seller_credit_score,title,nick,pic_url,num,list_time,stuff_status,location,price,has_discount,freight_payer,post_fee,express_fee,ems_fee,item_img,desc',
			'num_iids' => $iid,
			'outer_code' => 'miyifun',
		);

		$api = new TaobaoApi($newcfg);
		$result = $api->getSingleData($iid);
		$detail = $result['taobaoke_items_detail_get_response']['taobaoke_item_details']['taobaoke_item_detail'];
		//var_dump($detail);
		if(!$detail){
			$url = Yii::app()->params['tmallurl'];
			header("Location: $url");
			exit;
		}
		//分类导航
		$parent = Cates::model()->find('cid=:cid',array(':cid'=>$detail[0]['item']['cid']));
		//评论
		$commentcfg = array(
			'method' => 'taobao.traderates.search',
			'num_iid' => $iid,
			'seller_nick' =>$detail[0]['item']['nick'],
			'page_size' => 10,
			'outer_code' => 'miyifun',
		);
		$comapi = new TaobaoApi($commentcfg);
		$commentresult = $comapi->getSingleData();
		$comments = $commentresult['traderates_search_response']['trade_rates']['trade_rate'];
		//var_dump($comments);
		
		$this->render('detail',array('detail'=>$detail,'parent'=>$parent,'comments'=>$comments));
	}
}